/********
 * 发行折扣策略
 * 当调价类型发生改变的时候，调价明细中的datagrid 显示的列发生改变
 * @return
 */
function bindDiscount(){
	//$('#stj_addWinTable input[qucln=CLIENT_WHERE]').attr('placeholder','可点击进入选择');
	$('#stj_addWinTable input[qucln=CLIENT_WHERE]').attr({placeholder:'可单击进入选择'});
	$('#stj_addWinTable input[qucln=CLIENT_WHERE]').css('cursor','pointer');
	//获取调价类型
	var djTypeObj=$("#stj_addWinForm select[qucln=DJ_TYPE]");
	bindTjType(djTypeObj);
	$(djTypeObj).bind("change",function(){
		bindTjType(djTypeObj);
	});
	var clientObj = $("#stj_addWinForm input[qucln=CLIENT_WHERE]");
	$(clientObj).bind("click",function(){
		clickClient(clientObj);
	});
}
//调价类型的onchange事件
function bindTjType(djTypeObj){
//	var djType=$(djTypeObj).val();
//	if(djType==1){//调价格
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_PRICE_XS");   	//销售价
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_PRICE_LS");      //零售价
//		$("#stj_addItemDatagrid").datagrid("hideColumn","H_DISCOUNT_XS");   //销售折扣
//		$("#stj_addItemDatagrid").datagrid("hideColumn","H_DISCOUNT_LS");   //零售折扣
//	}else if(djType==2){//调折扣
//		$("#stj_addItemDatagrid").datagrid("hideColumn","H_PRICE_XS");   	//销售价
//		$("#stj_addItemDatagrid").datagrid("hideColumn","H_PRICE_LS");      //零售价
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_DISCOUNT_XS");   //销售折扣
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_DISCOUNT_LS");   //零售折扣
//	}else{
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_PRICE_XS");   	//销售价
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_PRICE_LS");      //零售价
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_DISCOUNT_XS");   //销售折扣
//		$("#stj_addItemDatagrid").datagrid("showColumn","H_DISCOUNT_LS");   //零售折扣
//	}
}
//客户条件的onclick事件
function clickClient(clientObj){
	var clientVal = showAllVal();
	var prefix = 'khw';
	var divName = 'Z_ADATAL'; 
	var dsId = 'ds_bs_clients_TC';
	var pdivName =prefix+'_Z_ADATAL';
	
	if($('#'+prefix+'_Z_save').length>0){
		$('#'+prefix+'_z_parentPanel').window('close');
		var obj = document.getElementById(prefix+"_Z_save");
		$('#'+prefix+'_z_parentPanel').remove();
		document.body.removeChild(obj);
	}
	
	var div_box = document.createElement("DIV");
	div_box.id = prefix+'_Z_save';
	div_box.height = 100;
	document.body.appendChild(div_box);
	
	$('#'+prefix+'_Z_save').load(_m_url+'singlePages/squeryFun',// singlePages/sinfoFun'
			{'dsId':dsId,'butnId':'','type':'3','staticWebUrl':'','initFunc':'','jspWebUrl':'/model/fxZkcl/fxzk','flag':'0'},
			function(){
				console.log($("#"+prefix+"_Z_AFORM"));
				$("#"+prefix+"_Z_AFORM").attr("sflag","s");
				$("#"+prefix+"_Z_AFORM").attr("pre",prefix);
				$.parser.parse('#'+prefix+'_z_saddDiv');
				$('#'+prefix+'_z_parentPanel').window({  
					height:window.parent.innerHeight-115,
					width:window.parent.innerWidth-180,
					left:'0',
					top:'0',
					draggable:false,
					modal:true,
					resizable:false,
					closable:false,
					collapsible:false,
					maximizable:false,
					minimizable:false
				}); 
				$('#'+prefix+'_z_parentPanel').window('open');
				$.ajax({
					type:'post',
					url:_m_url+'framework/loadColumnsU',
					data:{'colId':dsId,'type':'3'},
					success:function(data){
						if(data ==""){
							alert("您长时间未操作，系统已经自动退出!，请重新登陆");
						}else{
							producePage(data[0],prefix+"_"+divName,'5','');
							// 1.先清空数据该表单中所有的值,并初始化添加页面
							clearFormVal(pdivName);
							cEfect(pdivName,1);
							
							// 2.加载添加页面默认的数据(只有添加需要加载默认值，复制不需要)
							loadDefaultData(pdivName,dsId,"1","0");
							
//							if(clientVal != '' && clientVal != null){
//								fillInfo(pdivName,clientVal);
//							}
							showAllVal(pdivName);
						}
					}
				});
			});
}
//客户条件的确认按钮
function sureWhere(obj){
	var whereStr = "";
	var hanziStr = "";
	$("#khw_Z_AFORM input").each(function(){
		var coluname=$(this).attr('qucln');
		var clumVal = ''; //列的值
		var colum = ''; //列名
		var eleType = '';
		console.log(coluname);
		if(typeof(coluname) != 'undefined'&&coluname!= 'undefined'&&coluname!=""){
			console.log($(this).attr('eletype')+"-----"+$(this).attr('sm'));
			if($(this).attr('eletype') == "datalist" && $(this).attr('sm') == "false"){// 
				clumVal = $(this).parent().find("input[ismulcode=T]").val();//获取datalist选择值的编码
				var selVal = $(this).parent().find("input[ismulcode=T]").prev().val();//获取datalist的转译之后的值
				if(clumVal != '' && clumVal != null){
					var clumValList = clumVal.split(';');
					var str = "(";
					for(var i=0;i<clumValList.length;i++){
						if(clumValList[i] != '' && clumValList[i] != null){
							str += "'"+clumValList[i]+"',";
						}
					}
					str = str.substring(0,str.length-1);
					str = str+")";
					colum = $(this).parent().prev().html();
					whereStr = whereStr +coluname+" in "+str+" and ";
					hanziStr = hanziStr+colum +" '"+selVal+"'; ";
				}
			}else{
				clumVal =$(this).val();
				if(clumVal != '' && clumVal != null){
					colum = $(this).parent().prev().html();
					whereStr = whereStr+coluname+"='"+clumVal+"' and ";
					hanziStr = hanziStr+colum +" '"+clumVal+"'; ";
				}
			}
		}
	})
	whereStr = whereStr.substring(0,whereStr.lastIndexOf("and"));
	$('#khw_z_parentPanel').window('close');
	$('#stj_addWinForm input[qucln=CLIENT_WHERE]').val(whereStr);
	$('#stj_addWinForm input[qucln=CLIENT_WHERE]').attr('chan','T');
	$('#stj_addWinForm input[qucln=CLIENT_WHERE_NAME]').val(hanziStr);
	$('#stj_addWinForm input[qucln=CLIENT_WHERE_NAME]').attr('chan','T');
}
//获取界面上的值填充到客户条件窗口中
function showAllVal(formId){
	var fillData = {};
	var whereStr = $('#stj_addWinForm input[qucln=CLIENT_WHERE]').val();
	var allInObj = new Array(); 
	var setAllObj = new Array();
	if(whereStr != '' && whereStr != null){
		if(whereStr.indexOf("and") > 0){
			var strList = whereStr.split("and");
			for(var i=0;i<strList.length;i++){
				if(strList[i].indexOf("in")<0){
					var strList1 = strList[i].split("=");
					fillData[strList1[0].Trim()] = strList1[1].substring(1,strList1[1].lastIndexOf("'"));
				}else{
//					var str1 = strList[i].split('in');
//					var columnStr = str1[0].Trim(); //列名
//					if(columnStr == "CLIENTS_ID"){  //如果列名称为CLIENTS_ID
//						zyFunc = 'dz_clientsfmat';
//					}else{
//						zyFunc = 'dz_ctypefmat';
//					}
//					var subStrs = str1[1].Trim().substring(1,(str1[1].Trim()).length-1);//去掉括号
//					var subStrList = subStrs.split(",");
//					var str2 = '';
//					var str2Val = '';
//					for(var j = 0;j<subStrList.length;j++){
//						str2 += subStrList[j].substring(1,subStrList[j].length-1)+";"; //1;2;3
//						str2Val +=  eval("window.parent."+zyFunc+"('"+subStrList[j].substring(1,subStrList[j].length-1)+"')")+";";
////						alert(eval("window.parent."+zyFunc+"('"+subStrList[j].substring(1,subStrList[j].length-1)+"')"));
//					}
//					str2 = str2.substring(0,str2.length-1);
//					str2Val = str2Val.substring(0,str2Val.length);
//					allInObj[0]=columnStr;
//					allInObj[1]=str2;
//					allInObj[2] = str2Val;
					allInObj = getListFunc(strList[i]);
					setAllObj.push(allInObj);
				}
			}
		}else{
			if(whereStr.indexOf("in") > 0){
				allInObj = getListFunc(whereStr);
				setAllObj.push(allInObj);
			}else{
				var waitStr = whereStr.split("=");
				fillData[waitStr[0].Trim()] = waitStr[1].substring(1,waitStr[1].lastIndexOf("'"));
			}
		}
	}
	fillInfo(formId,fillData);
	$("#"+formId+" input").each(function(){
		var coluname=$(this).attr('qucln');
		for(var i =0;i<setAllObj.length;i++){
			if(coluname == setAllObj[i][0]){
				$(this).parent().find("input[ismulcode=T]").val(setAllObj[i][1]);
				$(this).parent().find("input[ismulcode=T]").prev().val(setAllObj[i][2]);
			}
		}
	})
}

function  getListFunc(whereStr){
	var allInObj = new Array(); 
	var zyFunc= ''; //转译方法
	var str1 = whereStr.split('in');
	var columnStr = str1[0].Trim(); //列名
	if(columnStr == "CLIENTS_ID"){  //如果列名称为CLIENTS_ID
		zyFunc = 'dz_clientsfmat';
	}else{
		zyFunc = 'dz_ctypefmat';
	}
	var subStrs = (str1[1].Trim()).substring(1,str1[1].Trim().length-1); //值 (去括号)
	var subStrList = subStrs.split(",");
	var str2 = '';
	var str2Val = '';
	for(var j = 0;j<subStrList.length;j++){
		str2 += subStrList[j].substring(1,subStrList[j].length-1)+";";
		str2Val +=  eval("window.parent."+zyFunc+"('"+subStrList[j].substring(1,subStrList[j].length-1)+"')")+";";
	}
	str2 = str2.substring(0,str2.length-1);
	str2Val = str2Val.substring(0,str2Val.length);
	allInObj[0]=columnStr;
	allInObj[1]=str2;
	allInObj[2] = str2Val;
	return allInObj;
}




